<template>
  <div>
    <div class="articlelist">
      <ul
        class="infinite-list"
        infinite-scroll-distance="40"
        v-infinite-scroll="load"
        infinite-scroll-disabled="disabled"
      >
        <li v-for="(item, index) in artileList" :key="item.id">
          <el-card
            v-if="index % 2 == 0"
            @click.native="toArticleDetail(item.id)"
          >
            <el-row>
              <el-col :span="12">
                <el-image
                  style="width: 100%; height: 280px"
                  :src="item.firstPicture"
                  fit="cover"
                  lazy
                >
                </el-image
              ></el-col>
              <el-col :span="12">
                <div class="articleinfo">
                  <h1>{{ item.title }}</h1>
                  <p>{{ item.description }}</p>
                  <div>
                    <i class="fa fa-calendar-check-o">{{
                      item.updateTime | dateFormat
                    }}</i>
                    <el-divider direction="vertical"></el-divider>
                    <i class="fa fa-folder">{{ item.category }}</i>
                    <el-divider direction="vertical"></el-divider>
                    <i class="fa fa-thumbs-o-up">{{
                      "(" + item.upCount + ")"
                    }}</i>
                  </div>
                  <div class="tag-container">
                    <i class="fa fa-tag"></i>
                    <el-tag
                      v-for="(tag, index) in item.tags"
                      :key="index"
                      class="tags"
                      >{{ tag }}</el-tag
                    >
                  </div>
                </div>
              </el-col>
            </el-row>
          </el-card>
          <el-card v-else @click.native="toArticleDetail(item.id)">
            <el-row>
              <el-col :span="12">
                <div class="articleinfo">
                  <h1>{{ item.title }}</h1>
                  <p>{{ item.description }}</p>
                  <div>
                    <i class="fa fa-calendar-check-o">{{
                      item.updateTime | dateFormat
                    }}</i>
                    <el-divider direction="vertical"></el-divider>
                    <i class="fa fa-folder">{{ item.category }}</i>
                    <el-divider direction="vertical"></el-divider>
                    <i class="fa fa-thumbs-o-up">{{
                      "(" + item.upCount + ")"
                    }}</i>
                  </div>
                  <div class="tag-container">
                    <i class="fa fa-tag"></i>
                    <el-tag
                      v-for="(tag, index) in item.tags"
                      :key="index"
                      class="tags"
                      >{{ tag }}</el-tag
                    >
                  </div>
                </div>
              </el-col>
              <el-col :span="12">
                <el-image
                  style="width: 100%; height: 280px"
                  :src="item.firstPicture"
                  fit="cover"
                  lazy
                >
                </el-image
              ></el-col>
            </el-row>
          </el-card>
        </li>
      </ul>
    </div>
    <div v-if="loading" class="loading">
      <i class="el-icon-loading"></i><span>加载中</span>
    </div>
    <div v-if="!loading" class="loading">
      <i class="el-icon-circle-close"></i><span>没有更多了</span>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: true,
      actived: true,
      artileList: [],
      current: 1,
      pageSize: 3,
      currentSize: 0,
      url: "https://lishungulimall.oss-cn-shenzhen.aliyuncs.com/config/a2a7d09169174dc61ea6bfdd33742fad.jpg",
      timer: null,
      sitetitle: "个人博客",
    };
  },
  methods: {
    init() {
      let params = {
        current: this.current,
        pageSize: this.pageSize,
      };
      this.postRequest("/api/article/home/list", params).then((res) => {
        this.artileList = this.artileList.concat(res.data.recodes);
        this.currentSize = res.data.currentSize;
      });
      this.loading = false;
    },
    toArticleDetail(id) {
      this.$router.push({
        name: "article",
        params: {
          id: id,
        },
      });
    },
    load() {
      let that = this;
      this.loading = true;
      setTimeout(() => {
        that.current++;
        let params = {
          current: that.current,
          pageSize: that.pageSize,
        };
        that.loading = false;
        that.postRequest("/api/article/home/list", params).then((res) => {
          that.artileList = this.artileList.concat(res.data.recodes);
          that.currentSize = res.data.recodes.length;
        });
      }, 250);
    },
    goTop() {
      document.documentElement.scrollTop = 0;
    },
    currentChange(page) {
      this.current = page;
      this.init();
    },
  },
  created() {
    this.init();
  },
  computed: {
    noMore() {
      return this.currentSize < this.pageSize;
    },
    disabled() {
      return !this.actived || this.loading || this.noMore;
    },
  },
  activated() {
    this.actived = true;
  },
  deactivated() {
    this.actived = false;
  },
};
</script>

<style lang="less" scoped>
.page {
  position: absolute;
  right: 50%;

  transform: translateX(-50%);
}
.articlelist {
  margin-top: 60px;
  margin-right: 40px;
  .el-card {
    height: 280px;
    border-radius: 12px !important;
    margin-bottom: 28px;
    border: 0 !important;
  }
}
.articleinfo {
  height: 100%;
  padding-left: 20px;
  h1 {
    font-size: 23px;
  }
  .read1 {
    position: absolute;
    bottom: 10px;
    right: 10px;
  }
  .read2 {
    position: absolute;
    bottom: 10px;
    left: 10px;
  }
}
.tag-container {
  margin-top: 20px;
}
.tags {
  margin: 0 5px;
}
.loading {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  height: 30px;
  font-size: 28px;
  line-height: 30px;
  i {
    vertical-align: middle;
  }
  span {
    vertical-align: middle;
  }
  span {
    font-size: 18px;
  }
}
li {
  list-style: none;
}
li:hover {
  cursor: pointer;
}
</style>